<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 学校管理
 */
class ArticleController extends AdminBaseController{
    // 定义数据表
    private $db;
    private $viewDb;

    // 构造函数 实例化ArticleModel表
    public function __construct(){
        parent::__construct();
        $this->db=D('Article');
    }

    //学校列表
    public function index(){
        $data=$this->db->getPageData('all','all','all',0,15);
        $this->assign('data',$data['data']);
        $this->assign('page',$data['page']);
        $this->display();
    }

    // 添加学校
    public function add(){
        if(IS_POST){
            if($aid=$this->db->addData()){

                $this->success('学校添加成功',U('Admin/Article/index'));
            }else{
                $this->error($this->db->getError());
            }
        }else{
            $allCategory=D('Category')->getAllData();
            if(empty($allCategory)){
                $this->error('请先添加分类');
            }
            $allTag=D('Tag')->getAllData();
            $this->assign('allCategory',$allCategory);
            $this->assign('allTag',$allTag);
            $this->display();
        }

    }

    // 向同步百度推送
    public function baidu_site($aid){
        $urls=array();
        $urls[]=U('Home/Index/article',array('aid'=>$aid),'',true);
        $api=C('BAIDU_SITE_URL');
        $ch=curl_init();
        $options=array(
            CURLOPT_URL=>$api,
            CURLOPT_POST=>true,
            CURLOPT_RETURNTRANSFER=>true,
            CURLOPT_POSTFIELDS=>implode("\n", $urls),
            CURLOPT_HTTPHEADER=>array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result=curl_exec($ch);
        $msg=json_decode($result,true);
        if($msg['code']==500){
            curl_exec($ch);
        }
        curl_close($ch);
    }

    // 修改学校
    public function edit(){
        if(IS_POST){
            if($this->db->editData()){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $aid=I('aid');
            $data=$this->db->getDataByAid($aid);
            $allCategory=D('Category')->getAllData();
            $allTag=D('Tag')->getAllData();
            $this->assign('allCategory',$allCategory);
            $this->assign('allTag',$allTag);
            $this->assign('data',$data);
            $this->display();
        }
    }

    // 彻底删除
    public function delete(){
        if($this->db->deleteData()){
            $this->success('彻底删除成功');
        }else{
            $this->error('彻底删除失败');
        }
    }


    // 导入excel
    function impSchool(){

        $tmp_file = $_FILES ['file_stu'] ['tmp_name'];
        $file_types = explode ( ".", $_FILES ['file_stu'] ['name'] );
        $file_type = $file_types [count ( $file_types ) - 1];

        /*判别是不是.xls文件，判别是不是excel文件*/
        if (strtolower ( $file_type ) != "xlsx" && strtolower ( $file_type ) != "xls")
        {
            $this->error ( '不是Excel文件，重新上传' );
        }

        /*设置上传路径*/
        $savePath = C('./Public/upload/');

        /*以时间来命名上传的文件*/
        $str = date ( 'Ymdhis' );
        $file_name = $str . "." . $file_type;

        /*是否上传成功*/
        if (! copy ( $tmp_file, $savePath . $file_name ))
        {
            $this->error ( '上传失败' );
        }

        $res=ExcelToArrary($savePath.$file_name,"UTF-8",$file_type);//传参,判断office2007还是office2003

        foreach ( $res as $k=>$v ) { //循环excel表
          if ($k>1){
              $data['title'] = $v [0];//创建二维数组
              $data['location'] = $v [1];
              $data['lnglat'] = $v [2];
              $data['student'] = $v [3];
              $data['teacher'] = $v [4];
              $data['price'] = $v [5];
              $data['salary'] = $v [6];
              $data['beginyear'] = $v [7];
              $data['acreage'] = $v [8];
              $data['verify'] = $v [9];
              $data['tids'] = explode(',',$v[10]);
              if(!empty($v[11])){
                  $data['c_name'] = explode(',',$v[11]);
              }
              if(!empty($v[12])){
                  $data['role'] = explode(',',$v[12]);
              }
              if(!empty($v[13])){
                  $data['tel'] = explode(',',$v[13]);
              }
              if(!empty($v[14])){
                  $data['wechat'] = explode(',',$v[14]);
              }
              D('Article')->addData($data);
          }
        }


        $this->success ( '导入成功' );


    }

}
